//
//  DENFindViewController.m
//  Icar
//
//  Created by apple on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "DENFindViewController.h"
#import "BaseHeader.h"
@interface DENFindViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic)UITableView *listTableView;//品牌tableView列表
@property (strong, nonatomic)UITableView *detailTableView;//品牌具体车型列表
@property (strong, nonatomic)UIView *detailView;//detailTableView所在的view;
@property (strong, nonatomic)UISwipeGestureRecognizer *swipeGesture;// 轻扫手势添加到detailTableView上;
@property (strong, nonatomic)UIButton *detailViewButton;//点击添加到detailView上;
@property (strong, nonatomic)NSMutableArray *allListArr;//所有列表中的汽车(不包括listHeader中数据)
@property (strong, nonatomic)NSMutableArray *allIndexArr;
//所有sectionTitle;
@property (strong, nonatomic)NSMutableArray *sectionsArr;
@property (strong, nonatomic)NSMutableArray *nameArr;//存放detailTableView中sectionTitle的数组
@property (strong, nonatomic)NSMutableArray *manufacturersArr;//存放数据
@property (strong, nonatomic)NSMutableArray *serialsArr;

@end

@implementation DENFindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBarTintColor:BLUECOLOR];
    self.title = @"找车";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"bar_btn_icon_search@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pushSearchView)];

    [self initializeAll];
    [self creatListTableView];
    [self creatDetailView];
    [self creatDetailViewButton];
    [self creatDetailTableView];
    [self creatSwipeGesture];
    [self addListHeader];
}
#pragma mark- 搜索按钮
- (void)pushSearchView
{
    DENSearchViewController *searchController = [[DENSearchViewController alloc]init];
    [self.navigationController pushViewController:searchController animated:YES];
}
#pragma mark- 初始化所有数组
- (void)initializeAll
{
    self.allListArr = [NSMutableArray array];
    self.allIndexArr = [NSMutableArray array];
    self.sectionsArr = [NSMutableArray array];
    self.serialsArr = [NSMutableArray array];
    self.nameArr = [NSMutableArray array];
}
#pragma mark- 创建listTableView
- (void)creatListTableView
{
    self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    签订tableView两个代理
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
//    数据没刷新出来以前取消cell分割线
    self.listTableView.separatorStyle = NO;
    [self.view addSubview:self.listTableView];
    
}
#pragma mark- 创建
- (void)creatDetailView
{
    self.detailView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //    设置detailView透明度
    self.detailView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    [self.view addSubview:self.detailView];
    
    
    
}
#pragma mark- 创建detailTableView
- (void)creatDetailTableView
{
    
//    初始化tableView
    self.detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH - WIDTH(100), SCREEN_HEIGHT - HEIGHT(64)) style:UITableViewStylePlain];
//    签订tableView协议
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
//    取消cell分割线
//    self.detailTableView.separatorStyle = NO;
//    将tableView添加到self.view上
    [self.view addSubview:self.detailTableView];
    self.detailTableView.alpha = 1;

}
#pragma mark- 创建清扫手势
- (void)creatSwipeGesture
{
//    初始化清扫手势
    self.swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(moveDetailView)];
//    设置清扫方向
    [self.swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
//    将清扫手势添加到detailTableView上
    [self.detailTableView addGestureRecognizer:self.swipeGesture];
}
#pragma mark- 创建点击button实现点击detailView时改变detailView的偏移量
- (void)creatDetailViewButton
{
//    初始化button
    self.detailViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    detailViewButton的大小与detailView的大小相同
    self.detailViewButton.frame = self.detailView.bounds;
    [self.detailViewButton addTarget:self action:@selector(moveDetailView) forControlEvents:UIControlEventTouchDown];
//    将button添加到detailView上
    [self.detailView addSubview:self.detailViewButton];
}
#pragma mark- 手势/按钮方法
- (void)moveDetailView
{
//    uiview 动画方法
    [UIView animateWithDuration:1 animations:^{
//        改变detailView坐标
        self.detailView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
//        改变detailTableView坐标
        self.detailTableView.frame = CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH - WIDTH(100), SCREEN_HEIGHT - HEIGHT(64));
    }];
}
#pragma mark- 上拉刷新
- (void)addListHeader
{
    __weak DENFindViewController *vc = self;
    [self.listTableView addLegendHeaderWithRefreshingBlock:^{
        
        [vc requestDataForPage:1 Isloadup:YES];
    }];
    [self.listTableView.header beginRefreshing];
}
#pragma mark- 请求数据
- (void)requestDataForPage:(NSInteger)page Isloadup:(BOOL)isloadup
{
//    判断是否是刷新状态
    if (isloadup == YES) {
        [self.allIndexArr removeAllObjects];
        [self.allListArr removeAllObjects];
    }
//    请求数据
    [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/price/brands?v=%ld.0.0", page] block:^(id back) {
//        解析数据
//        返回是一个字典
        self.sectionsArr = [back objectForKey:@"sections"];//获得sections数组
//     遍历sections数组取出brandsDic字典
        for (NSDictionary *brandsDic in _sectionsArr) {
//            获得index sectionTitle
            NSString *index = [brandsDic objectForKey:@"index"];
            [self.allIndexArr addObject:index];
        }
//        tableView 刷新
        [self.listTableView reloadData];
        //        让cell分割线出现
        self.listTableView.separatorStyle = YES;

//        下拉刷新结束
        [self.listTableView.header endRefreshing];
     
 }];
}
#pragma mark- 请求detailTableVeiw上的数据
- (void)requestDetailDataForNameId:(NSString *)nameId
{
    [self.serialsArr removeAllObjects];
    [self.nameArr removeAllObjects];
    [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/modelLibraryHomePage?bid=%@",nameId] block:^(id back) {
        self.manufacturersArr = [back objectForKey:@"manufacturers"];
        for (NSDictionary *manufacturersDic in _manufacturersArr) {
            NSString *name = [manufacturersDic objectForKey:@"name"];
            [self.nameArr addObject:name];
            NSMutableArray *serialArr = [manufacturersDic objectForKey:@"serials"];
            [self.serialsArr addObject:serialArr];
        }
        [self.detailTableView reloadData];
//        self.detailTableView.separatorStyle = YES;
    }];
}
#pragma mark- cell重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.listTableView) {
    static NSString *cellid = @"listCell";
    DENFindListTableViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (listCell == nil) {
        listCell = [[DENFindListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
//        解析数据
        NSMutableDictionary *brandsDic = [self.sectionsArr objectAtIndex:indexPath.section];
        NSMutableArray *brandsArr = [brandsDic objectForKey:@"brands"];
        NSMutableDictionary *singleLogoDic = [brandsArr objectAtIndex:indexPath.row];
        DENFindModel *findModel = [[DENFindModel alloc]init];
        [findModel setValuesForKeysWithDictionary:singleLogoDic];
//        传值
        listCell.findModel = findModel;
    return listCell;
    }else {
        static NSString *cellId = @"detailCell";
        DENFindDetailTableViewCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (detailCell == nil) {
            detailCell = [[DENFindDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
//        解析数据
        DENFindModel *findModel = [[DENFindModel alloc]init];
//        根据section取的这个section数组
        NSMutableArray *arr = [self.serialsArr objectAtIndex:indexPath.section];
//        根据row在数组中取的字典
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
//        kvc
        [findModel setValuesForKeysWithDictionary:dic];
//        传值
        detailCell.findModel = findModel;

        return detailCell;
    }
}
#pragma mark- 设置每个区中cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.listTableView) {
        if (self.sectionsArr != 0) {
        NSMutableDictionary *brandsDic = [self.sectionsArr objectAtIndex:section];
        NSMutableArray *brandsArr = [brandsDic objectForKey:@"brands"];
        return brandsArr.count;
        }
    }else if(tableView == self.detailTableView){
        if (self.serialsArr.count != 0) {
            NSMutableArray *arr = [self.serialsArr objectAtIndex:section];
            return arr.count;
        }
    }
    return 0;
}
#pragma mark- cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    if (tableView == self.listTableView) {
        NSMutableDictionary *brandsDic = [self.sectionsArr objectAtIndex:indexPath.section];
        NSMutableArray *brandsArr = [brandsDic objectForKey:@"brands"];
        NSMutableDictionary *singleLogoDic = [brandsArr objectAtIndex:indexPath.row];
        DENFindModel *findModel = [[DENFindModel alloc]init];
        [findModel setValuesForKeysWithDictionary:singleLogoDic];
        
//      动画  将detailView移动到当前view(屏幕)上;
    [UIView animateWithDuration:1 animations:^{
        self.detailView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        self.detailTableView.frame = CGRectMake(WIDTH(75), 64, SCREEN_WIDTH - WIDTH(75), SCREEN_HEIGHT - HEIGHT(108));
    }];
//        调用detail请求数据方法
        [self requestDetailDataForNameId:findModel.did];
//        self.detailTableView.separatorStyle = YES;
    }else{
        DENFindDetailViewController *detailViewController = [[DENFindDetailViewController alloc]init];
        [self.navigationController pushViewController:detailViewController animated:YES];
        
        //        根据section取的这个section数组
        NSMutableArray *arr = [self.serialsArr objectAtIndex:indexPath.section];
        //        根据row在数组中取的字典
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
//        将id传入DENFindDetailViewController中
        detailViewController.did = [dic objectForKey:@"id"];
        detailViewController.name = [dic objectForKey:@"name"];
        
    }
}
#pragma mark- 设置区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.listTableView) {
        return self.allIndexArr.count;
    }else{
        return self.nameArr.count;
    }
}
#pragma mark- 设置section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.listTableView) {
        return [self.allIndexArr objectAtIndex:section];
    }else
    {
        return [self.nameArr objectAtIndex:section];
    }
    
}
#pragma mark- 设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.listTableView) {
        return HEIGHT(60);
    }else{
        return HEIGHT(65);
    }
        
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (tableView == self.listTableView) {
        
        return self.allIndexArr;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
